package models

import (
	"admin-server/libs"
	"database/sql/driver"
	"fmt"
	"strings"
)

type Admin struct {
	libs.Model
	Username       string           `json:"username"`
	Password       string           `json:"password"`
	Nickname       string           `json:"nickname"`
	Avatar         string           `json:"avatar"`
	Phone          string           `json:"phone"`
	Status         AdminStatusType  `json:"status"`
	LoginIp        int              `json:"login_ip"`
	LoginTime      int              `json:"login_time"`
	LoginCount     int              `json:"login_count"`
	LoginFailTime  int              `json:"login_fail_time"`
	LoginFailCount int              `json:"login_fail_count"`
	Permission     AdminPermissions `gorm:"array" json:"permission"`
	DepartmentId   int              `json:"department_id"`
}

type AdminStatusType int32

const (
	// 可用
	AdminStatusEnabled AdminStatusType = 0
	// 禁用
	AdminStatusDisabled AdminStatusType = 1
)

type AdminPermissions []string

func (p *AdminPermissions) Value() (driver.Value, error) {
	return fmt.Sprintf("%v", p), nil
}

func (p *AdminPermissions) Scan(v interface{}) error {
	str := fmt.Sprintf("%s", v)

	*p = strings.Split(str, ",")

	return nil
}
